﻿<%@ Page Title="" Language="C#" MasterPageFile="~/admin/Admin.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_language_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript" defer="defer" > 
					$(document).ready(function() {
					var imgfilepath='../components/jqGrid/themes/basic/images/'; //define the path for the images
					jQuery("#listsg11").jqGrid({
						url:'../webservices/femr/index.php?item=services&service=City&action=get&q=1&letter=A',  //define the web service when the grid is loading
						datatype: "xml",
						height: 350,
						width: 1000,
						   colNames:['ID','City', 'State', 'Misc'],    //here we define the colums of the grid
						   colModel:[   //define the column properties
							   {name:'ID',index:'ID', width:35,sorttype:'int'},
							   {name:'City',index:'City', width:350,sortable:true,sorttype:'text'},
							   {name:'State',index:'State', width:350,sortable:true,sorttype:'text'},
							   {name:'Misc',index:'Misc', width:300, align:"left",sortable:true,sorttype:'text'},       
						   ],                                 // end define the column properties
						   rowNum:30,
						   rowList:[8,10,20,30,100],
						   imgpath: imgfilepath,
						   pager: jQuery('#pagersg11'), //set a name for the pager, the navigation bar
						   sortname: 'ID',  //define the default sorting column
						   viewrecords: true,
						   sortorder: "desc",   //define the default sorting direction
						   caption:"City",      //define the Caption
						   multiselect: true,
						   subGrid: false,
						   loadComplete: function()     //after the grid is loaded we set the links for updateing and define their functions
						   {
								var ids = $("#listsg11").getDataIDs();
								for (var i=0; i<ids.length; i++) {
									var rowId = ids[i];
									var rowData = $("#listsg11").getRowData(rowId);
									fileLink = "<a  id='"+rowId+"' class='href_track bold' href='#'>" + rowData["City"] +  "</a>";//here we define which row data
									$("#listsg11").setRowData(rowId, {City: fileLink})
								}
								$("#listsg11 a").click(function(e)  //define the click function, setting the modal popup
								{ 
									itemId=this.id;
									
									$('#insert_form').modal({onShow:modalShow, onOpen: modalOpen,onClose:modalClose});
									 
									 function modalShow(dialog)//function for seting size on the modal
									 {
										dialog.container.css({
										width: 480
									  });   
									 }
									 function modalOpen (dialog) {  //the function when the modal is opened
										dialog.overlay.fadeIn('fast', function () {
											dialog.container.fadeIn('fast', function () {
												dialog.data.hide().slideDown('fast'); 
												//////////////////////////////////////////////////////////
											 
											   //On Load Fill Form for Selected
												jQuery("#btn_save").val("Change");//set the name of the buttont to change
												
												var get_url="../webservices/femr/index.php?item=services&service=City&action=get&JSON=1&id="+itemId; //the web service for getting the selected data from the grid
												$.getJSON
												(
													get_url,
													function(json)
													{
														if(json[0][0] != null)
														{
															
															jQuery("#tbCity").val(json[0][0].City);
															jQuery("#tbState").val(json[0][0].State);
															jQuery("#tbMisc").val(json[0][0].Misc);
															jQuery("#cID").val(json[0][0].ID);
														}
														else
														{
															alert("Error while loading...");
														}
													}
												);
												
												//OnClick make update  for Selected
												jQuery("#btn_save").click(function()    //update function for the data 
												{   
													if(jQuery("#tbCity").val()!="" && jQuery("#tbState").val()!="")
													{
															var post_data="ID="+itemId+
															"&City="+jQuery("#tbCity").val()+
															"&State="+jQuery("#tbState").val()+
															"&Misc="+jQuery("#tbMisc").val();//set the data that we past in the post method via ajax
														/*$.ajax(
														{
															url:"../webservices/femr/index.php?item=services&service=City&action=update", // the web service for updateing the data
															type:"Post",
															data:post_data,
															success: function(msg){
																jQuery("#listsg11").trigger("reloadGrid");
																$.modal.close();
															}                                        
														}
														);*/
														var get_url="../webservices/femr/index.php?item=services&service=City&action=update&JSON=1&"+post_data; //the web service for update data 
														$.getJSON
														(
															get_url,
															function(json)
															{
																if(json[0][0] != null)
																{
																	if(json[0][0] == "Error") 
																	{
																		alert(json[0][1]);
																	}
																	if(json[0][0] == "Success")
																	{
																		alert(json[0][1]);  
																		jQuery("#listsg11").trigger("reloadGrid");
																		$.modal.close();  
																	} 
																	if(json[0][0] == "MissingData") 
																	{
																		mis_data_err = json[0][1];
																		for(i=2; i<json[0].length; i++)
																		{   
																			if(json[0][i] != "")
																			{
																				mis_data_err += "\n"+json[0][i];
																			}
																		}
																		alert(mis_data_err);
																	}                
																}
																else
																{
																	alert("Error while loading...");
																}
															}
														);
													}
													else
													{
														var errmsg = "";
														if(jQuery("#tbCity").val()=="") errmsg += "*Please input City Name.\n";
														if(jQuery("#tbState").val()=="") errmsg += "*Please input State.\n"; 
														alert(errmsg);
													}
													
												});
											 //////////////////////////////////////////////////////////
											});
										});
									}
									
									 function modalClose (dialog){
										dialog.data.fadeOut('fast', function () {
											dialog.container.hide('fast', function () {
												dialog.overlay.slideUp('fast', function () {
													itemId=null;
													$.modal.close();
													jQuery("#listsg11").trigger("reloadGrid");
												});
											});
										});                                                                                       
									}
									  
								});    
						   //end click
						////////////////////////////
					}
					})
					.navGrid('#pagersg11',{ refresh: false,add:false,edit:false,del:false,search:false })
					.navButtonAdd('#pagersg11',{ caption:"Add New", onClickButton: function()//define a button in the navigation bar for creating new data
					{  
						//Attach Modal Form for Insert New Record
						$('#insert_form').modal({onShow:modalShow, onOpen: modalOpen,onClose:modalClose});//definig the modal popup
						function modalShow(dialog)//define the size of the modal popup
						 {
							dialog.container.css({
							width: 480
						  });   
						 }
						function resetForm()    //reset the form
						{
							jQuery("#tbCity").val("");
							jQuery("#tbState").val("");
							jQuery("#tbMisc").val("");
							jQuery("#cID").val("");
						}
						function modalOpen (dialog) {
							dialog.overlay.fadeIn('fast', function () {
								dialog.container.fadeIn('fast', function () {
									dialog.data.hide().slideDown('fast'); 
									
									//////////////////////////////////////
									//Reset form
									resetForm();

									// Attach Event handler for Inserting New Records
									jQuery("#btn_save").click(function() 
									{ 
									   if(jQuery("#tbCity").val()!="" && jQuery("#tbState").val()!="")
									   {
										 //Send Ajax Request for Inserting New Record
										 var post_data="City="+jQuery("#tbCity").val()+
														"&State="+jQuery("#tbState").val()+
														"&Misc="+jQuery("#tbMisc").val();

											/*$.ajax(
												{
													url:"../webservices/femr/index.php?item=services&service=City&action=insert",
													type:"Post",
													data:post_data, 
													success: function(msg){
														alert("You have succesfully inserted data for new City");
														resetForm();
														jQuery("#listsg11").trigger("reloadGrid");
													}                                        
												}
											);*/
											var get_url="../webservices/femr/index.php?item=services&service=City&action=insert&JSON=1&"+post_data; //the web service for inserting new data 
											$.getJSON
											(
												get_url,
												function(json)
												{
													if(json[0][0] != null)
													{
														if(json[0][0] == "Error") 
														{
															alert(json[0][1]);
														}
														if(json[0][0] == "Success")
														{
															alert(json[0][1]);  
															resetForm();
															jQuery("#listsg11").trigger("reloadGrid"); 
														} 
														if(json[0][0] == "MissingData") 
														{
															mis_data_err = json[0][1];
															for(i=2; i<json[0].length; i++)
															{   
																if(json[0][i] != "")
																{
																	mis_data_err += "\n"+json[0][i];
																}
															}
															alert(mis_data_err);
														}  
													}
													else
													{
														alert("Error while loading...");
													}
													// close the dialog
													$.modal.close();
												}
											);
									   }
									   else
									   {
											var errmsg = "";
											if(jQuery("#tbCity").val()=="") errmsg += "*Please input City Name.\n";
											if(jQuery("#tbState").val()=="") errmsg += "*Please input State.\n"; 
											alert(errmsg);
//                                            alert("*Please input City name.");
									   }
									   return false;
									});
									//////////////////////////////////////
								});
							});
						}  
						  
					
					function modalClose (dialog){
						dialog.data.fadeOut('fast', function () {
							dialog.container.hide('fast', function () {
								dialog.overlay.slideUp('fast', function () {
									$.modal.close();
								});
							});
						});
					}
					
					}, position:"last" })
					.navButtonAdd('#pagersg11',{ caption:"Delete Selected", onClickButton: function()
					{
					   var s = jQuery("#listsg11").getGridParam("selarrrow");   
					   if (s != "")
					   {    
						   //Send Ajax Request for Deleting Selected Records
						   
						   $('#confirm').modal({
								close:false,
								overlayId:'confirmModalOverlay',
								containerId:'confirmModalContainer', 
								onShow: function (dialog) {
									// if the user clicks "yes"
									jQuery("#btn_yes").click(function () {
										 //Delete Selected rows
										$.ajax({
										   type: "POST",
										   url:'../webservices/femr/index.php?item=services&service=City&action=delete', 
										   data: "id="+s,
										   success: function(msg){
												jQuery("#listsg11").trigger("reloadGrid");  
										   }
										});
										// close the dialog
										$.modal.close();
									});
								}
							});
					   }
					   else
					   {
						   alert("Please select records to delete.");
					   }
					}, position:"last" })      
							.navButtonAdd("#pagersg11",{caption: 'A', buttonimg: '', 
										onClickButton: function()
										{   
											var new_url='../webservices/femr/index.php?item=services&service=City&action=get&letter='+this.caption;
											$("#listsg11").setGridParam({url:new_url,page:1});  
											$("#listsg11").trigger("reloadGrid"); 
										}, position: "last", title: '<?php echo $x;?>',reloadAfterSubmit:true})
							//insert the All Records button
							.navButtonAdd("#pagersg11",{caption: 'All Records', buttonimg: '', 
										onClickButton: function()
										{   
											var new_url='../webservices/femr/index.php?item=services&service=City&action=get&q=1';
											$("#listsg11").setGridParam({url:new_url,page:1});  
											$("#listsg11").trigger("reloadGrid"); 
										}, position: "last", title: 'All Records',reloadAfterSubmit:true})
							 ;  
							 
					});
					 </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table id="listsg11" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pagersg11" class="scroll" style="text-align:center;"></div>

</asp:Content>